<template>
  <div>
    <el-dialog :title="title"
               :visible.sync="isShow"
               destroy-on-close
               center
               width="40%"
               :before-close="modalClose">
      <el-form :model="form"
               :rules="rules"
               :inline-message="true"
               hide-required-asterisk
               label-width="120px"
               ref="form"
               size="mini"
               style="width: 90%">
        <el-form-item label="交易单号" prop="transactionNo">
          <el-input v-model="form.transactionNo" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="商户单号" prop="merchantOrderNo">
          <el-input v-model="form.merchantOrderNo" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="交易时间" prop="createTime">
          <el-date-picker
              v-model="form.createTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              :picker-options="pickerOptions"
              placeholder="选择日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="交易类型" prop="transactionType">
          <el-input v-model="form.transactionType" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="交易对方" prop="otherParty">
          <el-input v-model="form.otherParty" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="交易金额/元" prop="amount">
          <el-input type="number" v-model="form.amount" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="计费类型" prop="chargeType">
          <el-select v-model="form.chargeType" placeholder="计费类型">
            <el-option
                v-for="item in chargeType"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="交易状态描述" prop="stateDesc">
          <el-input v-model="form.stateDesc" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="交易状态" prop="state">
          <el-select v-model="form.state" placeholder="交易状态">
            <el-option
                v-for="item in state"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="服务费/元" prop="serviceFee">
          <el-input type="number" v-model="form.serviceFee" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="成功退款/元" prop="refundAmount">
          <el-input type="number" v-model="form.refundAmount" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="用户备注" prop="userRemark">
          <el-input v-model="form.userRemark" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="用户消费类别" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="类别">
            <el-option
                v-for="item in categoryId"
                :key="item.value"
                :label="item.label.categoryName"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeByEmit">取 消</el-button>
        <el-button type="primary" @click="onSubmit('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import getInfo from "../api/getInfo";
import add from "../api/add";
import update from "../api/update";

export default {
  name: "BillForm",
  props: {
    id: {
      type: String,
      default: ''
    },
    title: {
      type: String
    },
    visible: {
      type: Boolean
    },
    chargeType: {
      type: Array,
      default: () => []
    },
    state: {
      type: Array,
      default: () => []
    },
    categoryId: {
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      form: {
        transactionNo: '',
        merchantOrderNo: '',
        createTime: new Date().Format('yyyy-MM-dd HH:mm:ss'),
        transactionType: '',
        otherParty: '',
        goodsName: '',
        amount: 0,
        chargeType: '',
        stateDesc: '',
        state: '',
        serviceFee: 0,
        refundAmount: 0,
        remark: '',
        userRemark: '',
        categoryId: ''
      },
      rules: {
        transactionNo: [
          {required: true, message: '请输入交易单号', trigger: 'blur'},
          {max: 100, message: '长度100个字符以内', trigger: 'blur'}
        ],
        merchantOrderNo: [
          {max: 100, message: '长度100个字符以内', trigger: 'blur'}
        ],
        createTime: [
          {required: true, message: '请选择日期', trigger: 'blur'}
        ],
        amount: [
          {required: true, message: '请输入交易金额', trigger: 'change'},
        ],
        chargeType: [
          {required: true, message: '请选择计费类型', trigger: 'change'}
        ],
        state: [
          {required: true, message: '请选择交易状态', trigger: 'change'}
        ]
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date().Format('yyyy-MM-dd HH:mm:ss'));
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date().Format('yyyy-MM-dd HH:mm:ss');
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date().Format('yyyy-MM-dd HH:mm:ss');
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      }
    }
  },
  methods: {
    modalClose(done) {
      this.closeByEmit()
      done();
    },
    closeByEmit(isUpdate) {
      this.$emit('billFormClose', isUpdate);
    },
    async setBillInfo() {
      let res = await getInfo(this.id);
      if (res.success) {
        res.data.createTime = new Date(res.data.createTimeFormatter).Format('yyyy-MM-dd HH:mm:ss');
        res.data.amount = res.data.amountFormatter;
        res.data.serviceFee = res.data.serviceFeeFormatter;
        res.data.refundAmount = res.data.refundAmountFormatter;
        this.form = res.data;
      } else {
        this.closeByEmit()
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //时间值
          let date = this.form.createTime;
          //提交表单
          let res;
          if (this.id) {
            //更新
            res = await update(this.id, this.form);
          } else {
            res = await add(this.form);
          }
          if (res.success) {
            //关闭窗口
            this.closeByEmit(true);
            return ;
          }
          this.form.createTime = date;
        } else {
          return false;
        }
      });
    }
  },
  computed: {
    isShow: {
      get() {
        return this.visible;
      },
      set() {

      }
    }
  },
  beforeMount() {
    if (this.id) {
      this.setBillInfo();
    }
  }
}
</script>

<style scoped lang="less">
.el-form-item {
  margin-bottom: 5px;

  .el-input {
    width: 75%;
  }

  .el-select {
    width: 30%;
  }
}

</style>
